fn main() {
//     1.字符串
    println!("1.字符串");
// 一个只读内存中分配的字符串引用
    let pangram: &'static str = "the quick brown fox jumps over the lazy dog";
    println!("{pangram}");
//     逆序迭代单词,这里没有分配新的字符串
    println!("Words in reverse");
    for word in pangram.split_whitespace().rev() {
        println!("Reverse: > {word}");
    }
// 复制字符到一个vector中,排序并移除重复值
    let mut chars: Vec<char> = pangram.chars().collect();
    chars.sort();
    chars.dedup();
    println!("Chars the sort and dedup: > {chars:?}");
//     创建一个空的且可增长的字符串
    let mut string = String::new();
    for v in chars {
    //     在字符串尾部追加一个字符
        string.push(v);
    //     在字符串尾部追加一个字符串
        string.push_str(", ");
    }
    println!("New spawn string: > {string}");
//     去除空格
    let chars_to_trim: &[char] = &[' ', ','];
    let trimmed_str = string.trim_matches(chars_to_trim);
    println!("Trimmed string: > {trimmed_str}");
//     堆分配的字符串String::from,to_string,to_owned
//     let alice = String::from("I like dog");
//     let alice = "I like dog".to_string();
    let alice = "I like dog".to_owned();
//     分配新内存并存储修改过的字符串
    let bob = alice.replace("dog", "cat");
    println!("Alice says: {alice}, memory address: {:p}", &alice);
    println!("Bob says: {bob}, memory address: {:p}", &bob);

//     2.字面量与转义符
    println!("2.字面里与转义符");
//     通过转义,可以用十六进制值来表示字节
    let byte_escap = "I'm writing \x52\x75\x73\x74!";
    println!("What are you doing\x3F (\\x3F means ?) {byte_escap}");
//     也可以使用Unicode码位表示
    let unicode_codepoint = "\u{211D}";
    let character_name = "\"DOUBLE-STRUCK CAPITAL R\"";
    println!("Unicode character {unicode_codepoint} (U+211D) is called {character_name}");
//     多行字面量
    let long_str = "String literals
                            can span multiple lines.
                            The linebreak and indentation here ->\
                            <- can be escaped too!";
    println!("Lines string: > {long_str}");
//     使用原始字符串（raw string）。
    let raw_str = r"Escapes don't work here: \x3F \u{211D}";
    println!("Raw string: > {raw_str}");
//     在原始字符串中写引号,请在两边加一对#
    let quotes = r#"And then I said: "There is no escapes!""#;
    println!("Quotes: > {quotes}");
//     在字符串中需要写"#",在定界符中使用更多的#
//     可使用的#的数目没有限制
    let longer_delimiter = r###"A string with "# in it. And even "##!"###;
    println!("Longer delimiter: > {longer_delimiter}");

//     3.字节串
    println!("3.字节串");
//     这并不是一个&str
    let bytestring: &[u8; 20] = b"this is a bytestring";
//     字节串没有实现Display,所以它们的打印功能有些受限
    println!("A bytestring: {bytestring:?}");
//     字节串可以使用单字节的转义字符...
    let escaped = b"\x52\x75\x73\x74 as bytes";
//     ...但不能使用Unicode转义字符
//     let escaped = b"\u{211D} is not allowed";
    println!("Escaped bytes: {escaped:?}");
//     原始字节串和原始字符串的写法一样
    let raw_bytestring = br"\u{211D} is not escaped here";
    println!("Raw bytestring: {raw_bytestring:?}");
//     把字节串转换成为&str可能失败
    if let Ok(my_str) = std::str::from_utf8(raw_bytestring) {
        println!("And the same as text: '{my_str}'");
    }
    let quotes = br#"You can also use "fancier" formatting, \
                                like with normal raw strings"#;
    println!("Quotes: > {quotes:?}");
//     字节串可以不使用UTF-8编码
    let shift_jis = b"\x82\xe6\x82\xa8\x82\xb1\x82";
    println!("Shift-JIS: {shift_jis:?}");
//     但这样的话它们就无法转换成&str
    match std::str::from_utf8(shift_jis){
        Ok(my_str) => println!("Conversion successful: '{my_str}'"),
        Err(e) => println!("Conversion failed: {e}"),
    };
}
